﻿/**
* 封装Native提供给Js调用的接口
*/
var zs = new Zs();
function Zs(){
	var zs = {};
	_init.call(zs);
	return zs;
}

function _init(){

	window.setTitle=function(title){
        document.title=title;
        zs.setTitle({"title":title});
    };


	/*基础接口*/
	this.config = _config;
        this.ready = _ready;
	this.error = _error;
	this.checkJsApi = _checkJsApi;

	/**
		扩展接口
	*/
	this.getSSOToken = _getSSOToken;
	this.showStockHq = _showStockHq;
	this.setExtendBtn = _setExtendBtn;
	this.setTitle = _setTitle;
	this.getLocation = _getLocation;
	this.open = _open;
	this.close = _close;
	this.modifyUrl = _modifyUrl;
	this.getOwnStock = _getOwnStock;
	this.setBottomBtnType = _setBottomBtnType;
	this.showExtenBtnList = _showExtenBtnList;
	this.getAccount = _getAccount;
	this.getSkin = _getSkin;
	this.getInfo = _getInfo;
    this.setAppSkinStyle=_setAppSkinStyle;
    this.getPrice = _getPrice;
	this.commonTrade=_commonTrade;
	this.share=_share;
	this.captureImage=_captureImage;
	this.getSSOToken1 = _getSSOToken1;
	/**
	* 扩展事件
	*/
	this.onAppear = _onAppear;
	this.onDisappear = _onDisappear;
	this.onFresh = _onFresh;

	/**
	* 页面事件
	*/
	this.loadCompleted = _loadCompleted;

}
/**
	权限检查
	#param:js对象，包含以下属性：
		debug：bool类型，是否开启调试模式,调试模式下调用的所有api的返回值会在客户端alert出来
		busId：String类型，业务唯一标识，用于权限控制
		jsApiList：String数组，申请访问的js函数的列表，暂支持全部函数
  */
function _config(param){
	zs_bridge.isDebug = param.debug;
	zs_bridge.callFunc('config',
		{
			'busId':param.busId,
			'jsApiList':param.jsApiList
		},
		param.success,
		param.fail,
		param.complete);
}
/**
	配置完成时调用的接口
	#param:配置完成时调用的函数
  */
function _ready(param){
	zs_bridge.addFunc('ready',param);
}
/**
	配置完成时调用的接口
	#param:配置完成时调用的函数
  */
function _error(param){
	zs_bridge.addFunc('error',param);
}
/**
	权限检查
	#param:js对象，包含以下属性：
		jsApiList：String数组，待检查的js函数列表
  */
function _checkJsApi(param){
	zs_bridge.callFunc('checkJsApi',
		{
			'jsApiList':param.jsApiList
		},
		param.success,
		param.fail,
		param.complete);
}
function _getSSOToken(param){
	zs_bridge.callFunc('getSSOToken',
		{
			'type':param.type,
			'param':param.param,
			accType:param.accType
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle);
}
/**
	显示股票行情
	#param:js对象，包含以下属性：
		stock：js对象，某个股票代码{code:'600570',market:10}
  */
function _showStockHq(param){
	zs_bridge.callFunc('showStockHq',
		{
			'stock':param.stock
		},
		param.success,
		param.fail,
		param.complete);
}
/**
	获取股票行情
	#param:js对象，包含以下属性：
		stock：js对象，股票代码列表[{code:'600570',market:1},{code:'600571',market:1}]
  */
function _getPrice (param) {
	// body...
	zs_bridge.callFunc('getPrice',
		{
			'stock':param.stock
		},
		param.success,
		param.fail,
		param.complete);
}
/**
	获取地理位置信息
	#param:js对象
  */
function _getOwnStock(param){
	zs_bridge.callFunc('getOwnStock',
		{
		},
		param.success,
		param.fail,
		param.complete);
}
/**
	设置右上角操作按钮
	#param:js对象，包含以下属性：
		type：String类型，按钮的类型
		param：对应类型需要的参数
  */
function _setExtendBtn(param){
	zs_bridge.callFunc('setExtendBtn',
		{
			'type':param.type,
			'param':param.param
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
/**
	设置标题
	#param:js对象，包含以下属性：
		type：String类型，single,two,mutile,分表标识单个标题，2项选择标题，多个标题选择
		title：String数组，标题内容数组
		index:int类型，当前标题索引
  */
function _setTitle(param){
	zs_bridge.callFunc('setTitle',
		{
			'type':param.type,
			'title':param.title,
			'index':param.index,
			'subTitle':param.subTitle
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
/**
	获取地理位置信息
	#param:js对象
  */
function _getLocation(param){
	zs_bridge.callFunc('getLocation',
		{			
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
function _setBottomBtnType(param){
	zs_bridge.callFunc('setBottomBtnType',
		{
			type:param.type,
			param:param.param
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}

function _showExtenBtnList(param){
	zs_bridge.callFunc('showExtenBtnList',
		{
			param:param.param
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
function _getAccount(param){
	zs_bridge.callFunc('getAccount',
		{
			type:param.type,
			accType:param.accType,
			needLogin:param.needLogin
		},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
function _getSkin(param){
	zs_bridge.callFunc('getSkin',
		{},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}

function _getInfo(param){
	zs_bridge.callFunc('getInfo',
		{},
		param.success,
		param.fail,
		param.complete,
		param.cancle,
		param.trigger);
}
/**
	选择皮肤
	#param:js对象，包含以下属性：
		styleIndex:int(0：清新蓝，1：招商红)
  */
function _setAppSkinStyle(param){
	zs_bridge.callFunc('setAppSkinStyle',
		{
			'styleIndex':param.styleIndex
		},
		param.success,
		param.fail,
		param.complete);
}

function _open(param){
	if (typeof(param) == 'string') {
		this.open({target:'hybrid',param:{url:this.modifyUrl(param)}});
		return;
	};
	if(typeof(param) =="object" && !!param.param && !!param.param.url && !param.param.other){
    param.param.url= _modifyUrl(param.param.url);
    param.param.other=1;
  console.log(param);
    _open(param);
    return;
  }
	try{
		window.location.href="http://www.zsmodel.com/"+param.target+'?jsonParam='+encodeURIComponent(JSON.stringify(param.param));
		if(param.success != null){
			param.success(this,{errMsg:"open:ok"});
		}
	}
	catch(e){
		if(param.fail != null){
			param.fail.call(this,{errMsg:"调用失败，原因是：调用异常."});
		}
	}
	if(param.complete != null){
		param.complete.call(this,{errMsg:"open:ok"});
	}
}
function _modifyUrl(url){
	var pos = url.indexOf('/');
    if(pos == 0){
    	var host = location.protocol+'//'+location.host;    	
    	return host+url;
    }
    pos = url.indexOf("://");
    if(pos > 0){
    	return url;
    }
    pos = location.href.lastIndexOf('/');
    if(pos >= 0){
    	if(location.href.charAt(pos-1)=='/'){
    		return location.href + '/' + url;
    	}
    	return location.href.substr(0,pos+1)+url;
    }
    return location.href+'/'+url;
}


function _onAppear(param){
	zs_bridge.addFunc('appear',param);
}
function _onDisappear(param){
	zs_bridge.addFunc('disappear',param);
}
function _onFresh(param){
	zs_bridge.addFunc('fresh',param);
}
function _loadCompleted(param){
	zs_bridge.callFunc('loadCompleted');
}
function _close(param) {
	if (param == undefined) {
		zs_bridge.callFunc('close',{});
		return;
	}
	zs_bridge.callFunc('close', {
			stack: param.stack,
			code: param.code,
			msg: param.msg
		}
	);
}
	function _commonTrade(params){
		zs_bridge.callFunc('commonTrade',
			{
				'funcId':params.funcId,
				'param':params.param
			},
			params.success,
			params.fail,
			params.complete,
			params.cancle,
			params.trigger);

	}
function _share(param){
	zs_bridge.callFunc('share',
		{
			title:param.title,
			msgType:param.msgType,
			url:param.url,
			description:param.description,
			image:param.image,
			thunmb:param.thunmb,
			functions:param.functions
		},
		param.success,
		param.fail,
		param.complete,
		param.cancel,
		param.trigger);
}

function _captureImage(param){
zs_bridge.callFunc('captureImage',
  { type:param.type,
    maxSize:param.maxSize
  },
        param.success,
  		param.fail,
  		param.complete,
  		param.cancel,
  		param.trigger
  );
}

function _getSSOToken1(param){
	zs_bridge.callFunc('getSSOToken1',
		{	accType:param.accType,
			key:param.key
		},
		param.success,
		param.fail,
		param.complete,
		param.cancel,
		param.trigger
	);
}